<?php

/**
 * page actions.
 *
 * @package    cadsurvey
 * @subpackage page
 * @author     sharavdorj
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pageActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	if (!$request->getParameter('sf_culture'))
	  {
	    if ($this->getUser()->isFirstRequest())
	    {
	      $this->getUser()->setCulture('mn');
	      $this->getUser()->isFirstRequest(false);
	    }
	    else
	    {
	      $culture = $this->getUser()->getCulture();
	    }
	  }
    $this->pages = Doctrine::getTable('page')
      ->createQuery('a')
      ->execute();
    $this->slides = Doctrine::getTable('service')->findBy('slider', '1');
	  
  }

  public function executeCompany(sfWebRequest $request)
  {
    $this->page = Doctrine::getTable('page')
		->findBy('type','company');
  }
  public function executeGreeting(sfWebRequest $request)
  {
    $this->page = Doctrine::getTable('page')
		->findBy('type','greeting');
  }
  public function executeTimeline(sfWebRequest $request)
  {
	$this->timelinevalues = Doctrine::getTable('timeline')
		->createQuery('a')
		->orderBy('eventdate', 'ASC')
		->execute();
  }
  public function executeServices(sfWebRequest $request)
  {
  	$this->services = Doctrine::getTable('service')
      ->createQuery('a')
      ->execute();
  }
  public function executeService(sfWebRequest $request)
  {
    $this->service = $this->getRoute()->getObject();
  }
  public function executeProjects(sfWebRequest $request)
  {
  	$this->projects = Doctrine::getTable('project')
      ->createQuery('a')
      ->execute();
  }
  public function executeProject(sfWebRequest $request)
  {
  	$this->project = $this->getRoute()->getObject();
  }
  
  public function executeNews(sfWebRequest $request)
	{
		$this->newslist = Doctrine::getTable('news')
	      ->createQuery('a')
	      ->execute();
	}
  public function executeContact(sfWebRequest $request)
  {
  	$this->contact = Doctrine::getTable('contact')
  		->createQuery('a')
		->limit(1)
  		->execute();
	$this->feedbackForm = new FeedbackForm();
  }
  public function executeFeedback(sfWebRequest $request)
  {
		$this->forward404Unless($request->isMethod(sfRequest::POST));
  	//if ($request->isXmlHttpRequest())
      //{
          /*if ('*' == $query || !$this->contact)
            {
              return $this->renderText('Хайлтын үр дүн олдсонгүй.');
            }*/
        	//return $this->renderPartial('product/index', array('productss' => $this->productss));
        	//return $this->renderText('Таны хүсэлтийг бүртгэж авлаа.');
			$this->form = new FeedbackForm();
    
        	$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
	        if ($this->form->isValid())
	        {
	          $content = $form->save();
	          //$content->setCreatedAt(date('Y-m-d H:i:s'));
	          //$content->save();
	
	          $this->getUser()->setFlash('success', 'Амжилттай хадгалагдлаа.', true);
	          $this->redirect($request->getReferer() ? $request->getReferer() : 'page/contact');
	        }
      //}
  }
  
  public function executeChangelanguage(sfWebRequest $request)
  {
    $culture = in_array($request->getParameter('l'), array('en','mn')) 
    		? $request->getParameter('l') : 'mn';
        $this->getUser()->setCulture($culture);
        $this->getResponse()->setCookie('culture', $culture);
        
        $this->redirect($request->getReferer() ? $request->getReferer() : '@homepage');
  }

  public function executeGallery(sfWebRequest $request)
  {
  	
  	foreach (glob(sfConfig::get('sf_upload_dir').'/service/*.*') as $image) {
		$imgs[] = 'service/'.basename($image);	  
	  };
    foreach (glob(sfConfig::get('sf_upload_dir').'/*',GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
    	foreach (glob(sfConfig::get('sf_upload_dir').'/'.$dir.'/*.*') as $image) {
		$imgs[] = $dir.'/'.basename($image);	  
		  };
	  };  
  	$this->slides = $imgs;
  }
  
}
